package com.itheima.recyclerviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {

    private RecyclerView mRecyclerView;
    private List<DataBean> mDatas = new ArrayList<DataBean>();
    private List<DataBean> mStraggerDatas = new ArrayList<DataBean>();


    private int[] mListIcons = new int[]{R.mipmap.g1, R.mipmap.g2, R.mipmap.g3, R.mipmap.g4,
            R.mipmap.g5, R.mipmap.g6, R.mipmap.g7, R.mipmap.g8, R.mipmap.g9, R.mipmap.g10, R
            .mipmap.g11, R.mipmap.g12, R.mipmap.g13, R.mipmap.g14, R.mipmap.g15, R.mipmap
            .g16, R.mipmap.g17, R.mipmap.g18, R.mipmap.g19, R.mipmap.g20, R.mipmap.g21, R
            .mipmap.g22, R.mipmap.g23, R.mipmap.g24, R.mipmap.g25, R.mipmap.g26, R.mipmap
            .g27, R.mipmap.g28, R.mipmap.g29};

    private int[] mStraggeredIcons = new int[]{R.mipmap.p1, R.mipmap.p2, R.mipmap.p3, R
            .mipmap.p4, R.mipmap.p5, R.mipmap.p6, R.mipmap.p7, R.mipmap.p8, R.mipmap.p9, R
            .mipmap.p10, R.mipmap.p11, R.mipmap.p12, R.mipmap.p13, R.mipmap.p14, R.mipmap
            .p15, R.mipmap.p16, R.mipmap.p17, R.mipmap.p18, R.mipmap.p19, R.mipmap.p20, R
            .mipmap.p21, R.mipmap.p22, R.mipmap.p23, R.mipmap.p24, R.mipmap.p25, R.mipmap
            .p26, R.mipmap.p27, R.mipmap.p28, R.mipmap.p29, R.mipmap.p30, R.mipmap.p31, R
            .mipmap.p32, R.mipmap.p33, R.mipmap.p34, R.mipmap.p35, R.mipmap.p36, R.mipmap
            .p37, R.mipmap.p38, R.mipmap.p39, R.mipmap.p40, R.mipmap.p41, R.mipmap.p42, R
            .mipmap.p43, R.mipmap.p44};
    private SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //找到recyclerView
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);


        initData();
        initListAdapterV();

        initListener();

    }

    private void initListener() {
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //模拟加载数据
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SystemClock.sleep(2000);
                        MainActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //停止刷新操作
                                mSwipeRefreshLayout.setRefreshing(false);
                                //得到adapter.然后刷新
                                mRecyclerView.getAdapter().notifyDataSetChanged();
                            }
                        });

                    }
                }).start();
                ;
            }
        });
    }

    private void initListAdapterV() {
        //设置layoutManager
        LinearLayoutManager layoutManger = new LinearLayoutManager(MainActivity.this);
        mRecyclerView.setLayoutManager(layoutManger);

        //设置adapter
        ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);
        mRecyclerView.setAdapter(adapter);
    }

    private void initListAdapterH() {
        //设置layoutManager
        LinearLayoutManager layoutManger = new LinearLayoutManager(MainActivity.this,
                LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(layoutManger);

        //设置adapter
        ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);
        mRecyclerView.setAdapter(adapter);
    }

    private void initGridAdapterV() {
        //设置layoutManager
        LinearLayoutManager layoutManger = new GridLayoutManager(MainActivity.this, 2);
        mRecyclerView.setLayoutManager(layoutManger);

        //设置adapter
        ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);
        mRecyclerView.setAdapter(adapter);
    }

    private void initGridAdapterH() {
        //设置layoutManager
        LinearLayoutManager layoutManger = new GridLayoutManager(MainActivity.this, 2,
                LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(layoutManger);

        //设置adapter
        ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);
        mRecyclerView.setAdapter(adapter);
    }

    private void initStaggeredGridAdapterV() {
        //设置layoutManager
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(staggeredGridLayoutManager);

        //设置adapter
        StraggerAdapter adapter = new StraggerAdapter(MainActivity.this, mStraggerDatas);
        mRecyclerView.setAdapter(adapter);
    }

    private void initStaggeredGridAdapterH() {
        //设置layoutManager
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(staggeredGridLayoutManager);

        //设置adapter
        StraggerAdapter adapter = new StraggerAdapter(MainActivity.this, mStraggerDatas);
        mRecyclerView.setAdapter(adapter);
    }

    private void initData() {
        for (int i = 0; i < mListIcons.length; i++) {
            int iconId = mListIcons[i];
            DataBean dataBean = new DataBean();
            dataBean.iconId = iconId;
            dataBean.text = "我是item" + i;
            mDatas.add(dataBean);
        }
        for (int i = 0; i < mStraggeredIcons.length; i++) {
            int iconId = mStraggeredIcons[i];
            DataBean dataBean = new DataBean();
            dataBean.iconId = iconId;
            dataBean.text = "我是item" + i;
            mStraggerDatas.add(dataBean);

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_list_v:
                initListAdapterV();
                break;
            case R.id.action_list_h:
                initListAdapterH();
                break;
            case R.id.action_grid_v:
                initGridAdapterV();
                break;
            case R.id.action_grid_h:
                initGridAdapterH();
                break;
            case R.id.action_stragger_v:
                initStaggeredGridAdapterV();
                break;
            case R.id.action_stragger_h:
                initStaggeredGridAdapterH();
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
